var app = getApp();

Page({
  data: {
    addressListData: [],
    state: 0,
    show_add_address:true
  },
  // 获取收货地址列表
  _getData: function () {
    var that = this;
    app._getNetWork({
      showToastIn: false,
      url: app.getNetAddresss("member.member-address.index"),
      success: function (res) {
        var _data = res.data;
        if (_data.result == 1) {
          var _state = 0;
          if (_data.data.length <= 0) {
            _state = 1;
          } else {
            _state = 2;
          }
          that.setData({
            addressListData: _data.data,
            state: _state,
          });
          console.log("收货地址列表", _data.data);
        } else {
          wx.showToast({
            title: "数据错误",
            icon: "success",
            duration: 2000,
            success: function () {},
            fail: function () {},
          });
        }
      },
      fail: function (res) {},
    });
  },
  // 设置默认收货地址
  defaultAddress: function (e) {
    var that = this;
    var url = app.getNetAddresss("member.member-address.setDefault");
    url += "&address_id=" + e.currentTarget.dataset.id;
    app._getNetWork({
      url: url,
      success: function (res) {
        var _data = res.data;
        console.log("设置默认收货地址", res);
        if (_data.result == 1) {
          that._getData();
        } else {
          wx.showToast({
            title: _data.msg,
            icon: "none",
            duration: 2000,
            success: function () {},
            fail: function () {},
          });
        }
      },
      fail: function (res) {},
    });
  },
  // 修改收货地址
  editAddress: function (e) {
    var _item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url:
        "/packageD/member/addressEdit/addressEdit?model=" +
        JSON.stringify(_item),
    });
  },
  // 删除收货地址
  delAddress: function (e) {
    var that = this;
    var url = app.getNetAddresss("member.member-address.destroy");
    url += "&address_id=" + e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "确定要删除么？此操作不可逆！",
      success: function (res) {
        if (res.confirm) {
          console.log("用户点击确定");
          app._getNetWork({
            url: url,
            success: function (res) {
              var _data = res.data;
              console.log("收货地址列表", _data);
              if (_data.result == 1) {
                that._getData();
                wx.showToast({
                  title: _data.msg,
                  icon: "success",
                  duration: 2000,
                  success: function () {},
                  fail: function () {},
                });
              } else {
                wx.showToast({
                  title: "数据错误",
                  icon: "success",
                  duration: 2000,
                  success: function () {},
                  fail: function () {},
                });
              }
            },
            fail: function (res) {},
          });
        }
      },
    });
  },

  _gotoAddressAdd: function (options) {
    let defaultIs = true;
    if(this.data.addressListData.length >0){
      for(let i = 0;i<this.data.addressListData.length;i++){
        if(this.data.addressListData[i].isdefault == 1){
          defaultIs = false;
        }
      }
    }
    wx.navigateTo({
      url: "/packageD/member/addressAdd_v2/addressAdd_v2?isdefault="+defaultIs,
    });
  },
  updateAdress(){
    wx.showLoading({
      title: '更新缓存中',
    })
    wx.removeStorage({
      key: 'provinceData',
      success (res) {
        console.log(res)
        wx.hideLoading({
          success: (res) => {
            wx.showToast({
              title: '更新成功',
              icon:'none',
              duration:1000
            })
          },
        })
      },
      fail(res){
        wx.hideLoading({
          success: (res) => {
            wx.showToast({
              title: '更新失败',
              icon:'none',
              duration:1000
            })
          },
        })
      }
    })
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    this._getData();
    let yz_basic_info = wx.getStorageSync("yz_basic_info");
    if(yz_basic_info.plugin_setting && yz_basic_info.plugin_setting.address_code){
      let address_code = yz_basic_info.plugin_setting.address_code;
      this.setData({
        show_add_address:address_code.show_add_address
      })
    }
    this.setData({
      address_parse_open:yz_basic_info.plugin_setting.address_parse_open?yz_basic_info.plugin_setting.address_parse_open:0
    })
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
});
